package com.io.util;

import java.io.IOException;
import java.io.InputStream;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * Created by yangyx on 2016/8/1.
 */
public class Base64Utils {
	/**
	 * 方法说明：编码方法, 把一个字节数组编码为BASE64编码的字符串
	 *
	 * @param arrB
	 * @return
	 */
	public static String encode(byte[] arrB) {
//		AssertUtils.notNullAndEmpty(arrB);
		return new BASE64Encoder().encode(arrB);
	}

	/**
	 * 方法说明：解码方法, 把一个BASE64编码的字符串解码为编码前的字节数组
	 * @param in
	 * @return
	 * @throws IOException 
	 */
	public static byte[] decode(String in) throws IOException {
//		AssertUtils.notNullAndEmpty(in);
		byte[] arrB = null;
		arrB = new BASE64Decoder().decodeBuffer(in);
		return arrB;
	}

	/**
	 * 方法说明：编码方法, 把InputStream流中的数据内容编码为BASE64编码的字符串
	 * @param in
	 * @return
	 * @throws IOException 
	 */
	public static String encode(InputStream in) throws IOException {
//		AssertUtils.notNullAndEmpty(in);
		byte[] read = new byte[64];
		StringBuffer result = new StringBuffer("");
		int bytesRead = -1;
		while ((bytesRead = in.read(read)) != -1) {
			byte[] src = read;
			if (bytesRead != 64) {
				src = new byte[bytesRead];
				for (int j = 0; j < bytesRead; j++) {
					src[j] = read[j];
				}
			}
			result.append(Base64Utils.encode(src));
		}
		return result.toString();
	}

	public static void main(String args []){
		System.out.println("start");
		try {
			System.out.println(encode("12345678".getBytes()));
			System.out.println(encode("1234567811".getBytes()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("end");
	} 
}
